﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL;

namespace UAS5.UControls
{
    /// <summary>
    /// 超级命令行
    /// </summary>
    /// <remarks>2014-2-12 by sutroon</remarks>
    public partial class ucSoCommandLine : UserControl
    {
        AppDataHelper adh;
        CommandHelper ch;
        public ucSoCommandLine()
        {
            InitializeComponent();

            ch = new CommandHelper();

            ch.OuterCommandHandler += OuterCommandEvent;
            btnRun.Click += new EventHandler(btnRun_Click);
            cbbCommand.KeyPress += new KeyPressEventHandler(cbbCommand_KeyPress);
        }

        public void uc_init()
        {
            adh = new AppDataHelper();
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            RunCmd();
        }
        private void cbbCommand_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar ==(char)Keys.Enter) RunCmd();
        }

        private void RunCmd()
        {
            if (cbbCommand.Text.Length == 0) return;
            ch.Run(cbbCommand.Text);
        }

        private void OuterCommandEvent(string cmd)
        {
            DataTable dt = adh.ExecuteDataTable(string.Format("select a.Paths,b.Content from tquickdial a left join tgeneralcatalog b on a.Tags=b.Name where a.Name='{0}'", cmd));
            string paths = dt.GetValue<string>(r => { return r["Paths"];});
            if (paths.Length == 0) return;
            switch (dt.GetValue<string>(r => { return r["Content"]; }))
            {
                case "文件":
                    paths = FileHelper.GetExistsPath(paths.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
                    if (paths.Length == 0) return;
                    System.Diagnostics.Process.Start(paths);
                    break;
                case "网址":
                    System.Diagnostics.Process.Start(paths);
                    break;
                case "目录":
                    paths = FileHelper.GetExistsPath(paths.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries), "folder");
                    if (paths.Length == 0) return;
                    System.Diagnostics.Process.Start(paths);
                    break;
            }
        }
    }
}
